package com.mudalov.rec.dao.model.study;

import com.mudalov.rec.dao.interfaces.IClonable;
import com.mudalov.rec.util.StringUtil;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Question implements Serializable, IClonable<Question> {
	
	private static final long serialVersionUID = 4337327843042263796L;

    public Question getClone() {
        Question clone = new Question();
        clone.setId(this.getId());
        clone.setText(this.getText());
        clone.setType(this.getType());
        clone.setAnswer(this.getAnswer());
        for (String string : variants) {
            clone.getVariants().add(string);
        }
        return clone;
    }

	public static enum QuestionType {RADIO, CHECKBOX};

	private Long id;
	
	private String text;
	
	private String answer;
	
	private String type;
	
	private Set<String> variants;

	
	public Question() {
        variants = new HashSet<String>();
        type = Question.QuestionType.CHECKBOX.toString();
    }
	
	public Question(String text, String answer, QuestionType type,
			Set<String> variants) {
		this.text = text;
		this.answer = answer;
		this.type = type.toString();
		this.variants = variants;
	}

    public String getShortText() {
        if (text != null && text.length() > 50) {
            return text.substring(0, 50) + " ...";
        } else {
            return text;
        }
    }

    public String getVariantsString() {
        StringBuilder sb = new StringBuilder();
        for (String string : variants) {
            sb.append(string + ";");
        }
        return sb.toString();
    }

    public void setVariantsString(String variantsString) {
        variants.clear();
        if (!StringUtil.areEmpty(variantsString)) {
            String[] vars = variantsString.split(";");
            for (String var : vars) {
                variants.add(var);
            }
        }
    }
	
	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}

	public String getType() {
		return type;
	}
	
	public void setType(String type) {
		this.type = type;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public Set<String> getVariants() {
		return variants;
	}

	public void setVariants(Set<String> variants) {
		this.variants = variants;
	}

	public void setType(QuestionType type) {
		this.type = type.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((answer == null) ? 0 : answer.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((text == null) ? 0 : text.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		result = prime * result
				+ ((variants == null) ? 0 : variants.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Question))
			return false;
		Question other = (Question) obj;
		if (answer == null) {
			if (other.answer != null)
				return false;
		} else if (!answer.equals(other.answer))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (text == null) {
			if (other.text != null)
				return false;
		} else if (!text.equals(other.text))
			return false;
		if (type == null) {
			if (other.type != null)
				return false;
		} else if (!type.equals(other.type))
			return false;
		if (variants == null) {
			if (other.variants != null)
				return false;
		} else if (!variants.equals(other.variants))
			return false;
		return true;
	}
}
